<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\Certificate;
use App\Models\ExamPaper;
use App\Services\CertificateService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CertificateController extends AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'certificate_category_id' => 'required|integer|digits_between:1,20|min:1',
            'image_path' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method', '_token']);
        $id = $request->input('id');
        $flight = Certificate::updateOrCreate(
            ['id' => $id],
            $putData
        );
        return $this->success($flight->id);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $certificate_category_id = $request->input('certificate_category_id');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = CertificateService::getInstance()->list($page,$limit,$sort,$order, $name,$certificate_category_id);
        return $this->success($list);
    }
    //未分配的考试的证书列表
    public function  allArr(Request $request){
        // 查找试卷
        $certificateIds = ExamPaper::pluck('certificate_id')->toArray();
        if (!$certificateIds) {
            $certificateIds =  [-1];
        }
        $certificateIds = array_unique($certificateIds, SORT_REGULAR);
        $arr = Certificate::whereNotIn('id',$certificateIds)->select('id','name')->get();
        return $this->success($arr);
    }
//删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $certificate = Certificate::find($request->id);
        $certificate->delete();
        return $this->success();
    }
    // 图上传
    public function img(Request $request)
    {
        $url = config('filesystems.disks.certificate.url');
        $default = config('filesystems.disks.certificate.default');
        $file = CertificateService::getInstance()->upfile($request, $url, $default, 'certificate','img');
        if(!$file){
            return $this->fail(CodeResponse::IMAGE_INCORRECT_FORMAT,'不支持当前图片扩展名');
        }
        return $this->success([
                'file' => $file,
            ]
        );
    }
}
